<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>系统参数设置</title>
<%@ include file="/common/header/meta.jsp"%>
<%@ include file="/common/header/script.jsp"%>
<script type="text/javascript">
	var tabsContainer, $dg, database, $dg_rev ;
	$(function() {
		tabsContainer = $("#tabsContainer").tabs({
			fit: true, border: false, tabPosition: 'bottom',
			onSelect: function(title, index) {
				if("数据库恢复" == title) {
					$dg_rev.datagrid("reload") ;
				}
			}
		}) ;
		
		$dg = $("#dg").datagrid({
			url: base.contextPath+"/sysmgr/dbmgr/doNotNeedAuth_tablesInformation.do",
			title: '<span style="font-weight:normal;color:#666;">系统基础配置-->系统管理-->数据库管理</span>', 
			idField: 'TABLE_NAME', fit: true, border: false, rownumbers: true,
			remoteSort: false, toolbar: '#toolbars', striped:true, singleSelect: true,
			frozenColumns: [[
			    { field: 'ck', checkbox: true }
			]],
			columns: [[
			    { field: 'TABLE_NAME', title: '表名称', width: 200, sortable: true },
			    { field: 'TABLE_ROWS', title: '记录数', width: 100, sortable: true },
			    { field: 'ENGINE', title: '表类型', width: 100, sortable: true },
			    { field: 'TABLE_COLLATION', title: '字符编码', width: 120, sortable: true }, 
			    { field: 'CREATE_TIME', title: '创建时间', width: 140, sortable: true },
			    { field: 'action', title: '操作', width: 270, formatter: function(value, row){
			    	var str = "" ;
			    	str += $.string.format("<a href='javascript:;' onclick='tableDatail(\"{0}\");' class='clickLInk ext_sitemap'>表结构</a>", row.TABLE_NAME) ;
			    	str += $.string.format("<a href='javascript:;' onclick='tableSQL(\"{0}\");' class='clickLInk ext_script'>SQL脚本</a>", row.TABLE_NAME) ;
			    	return str ;
			    }}
			]]
		}) ;
		
		$dg_rev = $("#dg_rev").datagrid({
			url: base.contextPath+"/sysmgr/dbmgr/doNotNeedAuth_datagrid.do",
			title: '<span style="font-weight:normal;color:#666;">系统基础配置-->系统管理-->数据库恢复</span>', 
			idField: 'id', fit: true, border: false, rownumbers: true,
			remoteSort: false, toolbar: '#toolbars_rev', striped:true, singleSelect: true,
			frozenColumns: [[
			    { field: 'ck', checkbox: true },
			    { field: 'id', title: 'ID', hidden: true}
			]],
			columns: [[
			    { field: 'databaseName', title: '数据库', width: 200, sortable: true },
			    { field: 'created', title: '创建时间', width: 140, sortable: true }
			]]
		}) ;
		
		database = $("#database").combobox({
			url: base.contextPath+"/sysmgr/dbmgr/doNotNeedAuth_databases.do", disabled: true,
			valueField:'SCHEMA_NAME', textField:'SCHEMA_NAME', value: '${databaseName}',
			onSelect: function(rec) {
				 $dg.datagrid("load", {databaseName: rec.SCHEMA_NAME}) ;
			}
		});
		
	});
	
	function tableDatail(table) {
		$.easyui.showDialog({
			href: base.contextPath+"/sysmgr/dbmgr/doNotNeedAuth_table_datail.do?databaseName="+database.combobox("getValue")+"&table="+table, title: "表详细信息", 
			iniframe: false, topMost: true, maximizable: true,
			width: 750, height: 400,enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false
        });
	}
	function tableSQL(table) {
		$.easyui.showDialog({
			href: base.contextPath+"/sysmgr/dbmgr/doNotNeedAuth_table_sql.do?databaseName="+database.combobox("getValue")+"&table="+table, title: "SQL脚本", 
					iniframe: true, topMost: true, maximizable: true,
			width: 750, height: 400,enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false
        });
	}
	
	function backupDatabase() {
		$.post(base.contextPath+"/sysmgr/dbmgr/backupDatabase.do?", {modifyName: "${USER_SESSION.truename}", databaseName: database.combobox("getValue")}, function(result) {
			if(result.status) {
				alertify.success(result.msg);
			} else{
				$.messager.alert("错误", result.msg, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	}
	
	function recoverDatabase() {
		var row = $dg_rev.datagrid("getSelected");
		if(row) {
			$.messager.confirm("您确定要进行该操作？", function (c) { 
				if(c) {
					$.post(base.contextPath+"/sysmgr/dbmgr/recoverDatabase.do?", {id: row.id}, function(result) {
						if(result.status) {
							alertify.success(result.msg);$dg_rev.datagrid('unselectAll');
						} else{
							$.messager.alert("错误", result.msg, "error");
						}
					}, 'json').error(function() { $.easyui.loaded(); });
				}
			});
		} else {
			alertify.warning("请选择需恢复的文件！");
		}
	}
	
	function deleteBackup() {
		var row = $dg_rev.datagrid("getSelected");
		if(row) {
			$.messager.confirm("您确定要进行该操作？", function (c) { 
				if(c) {
					$.post(base.contextPath+"/sysmgr/dbmgr/deleteBackup.do?", {id: row.id}, function(result) {
						if(result.status) {
							alertify.success(result.msg);$dg_rev.datagrid('reload');$dg_rev.datagrid('unselectAll');
						} else{
							$.messager.alert("错误", result.msg, "error");
						}
					}, 'json').error(function() { $.easyui.loaded(); });
				}
			});
		} else {
			alertify.warning("请选择需恢复的文件！");
		}
	}
	
	function downBackup() {
		var row = $dg_rev.datagrid("getSelected");
		if(row) {
			//$.messager.confirm("您确定要进行该操作？", function (c) { 
			//	if(c) {
					window.location.target="_ablank" ; 
					window.location.replace(base.contextPath+"/sysmgr/dbmgr/download.do?folder="+row.folder+"&databaseFullName="+encodeURI(encodeURI(row.databaseFullName)));
			//	}
			//});
		} else {
			alertify.warning("请选择一条记录！");
		}
	}
</script>
</head>

<body>
<div id="tabsContainer">
	<div class="panel-container" data-options="title: '数据库管理', iconCls: 'icon-standard-layout-header', refreshable: false, selected: true">
		<div id="dg">
			<div id="toolbars">
	           	 数据库：<input id="database" name="database" style="width:180px;" />
	            <a onClick="backupDatabase();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_remove'">备份数据库</a>
	            <a onclick="$dg.datagrid('reload');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_reload'">刷新</a>
	        </div>
		</div>
	</div>
	
	<div class="panel-container" data-options="title: '数据库恢复', iconCls: 'icon-standard-layout-header', refreshable: false, selected: true">
		<div id="dg_rev">
			<div id="toolbars_rev">
	            <a onClick="recoverDatabase();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_edit'">恢复</a>
	            <a onClick="deleteBackup();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_edit'">删除</a>
	            <a onclick="$dg_rev.datagrid('reload');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_reload'">刷新</a>
	            <a onClick="downBackup();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'icon-hamburg-down'">下载</a>
	        </div>
		</div>
	</div>
	
</div>

</body>
</html>